﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.Web;
using System.Web.Mvc;
using SalesPortal.Web.Models;

namespace SalesPortal.Web.Controllers
{
    public class ProjectBlockController : Controller
    {
        private SalesPortalEntities db = new SalesPortalEntities();

        // GET: ProjectBlock
        public async Task<ActionResult> Index(int id)
        {
            var projectBlocks = db.ProjectBlocks.Where(r => r.ProjectId == id).Include(p => p.Project);

            return View(new BlockListViewModel
            {
                ProjectId = id,
                BlockList = await projectBlocks.ToListAsync()
            });

            //return View(await projectBlocks.ToListAsync());
        }

        // GET: ProjectBlock/Details/5
        public async Task<ActionResult> Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ProjectBlock projectBlock = await db.ProjectBlocks.FindAsync(id);
            if (projectBlock == null)
            {
                return HttpNotFound();
            }
            return View(projectBlock);
        }

        // GET: ProjectBlock/Create
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">Project Id</param>
        /// <returns></returns>
        public ActionResult Create(int id)
        {
            ViewBag.ProjectId = new SelectList(db.Projects, "Id", "Name");

            return View(new SalesPortal.Web.Models.ProjectBlock
            {
                ProjectId = id
            });
        }

        // POST: ProjectBlock/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include = "Id,ProjectId,BlockNo,DoorPlateBlockNo")] ProjectBlock projectBlock)
        {
            if (ModelState.IsValid)
            {
                db.ProjectBlocks.Add(projectBlock);
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            ViewBag.ProjectId = new SelectList(db.Projects, "Id", "Name", projectBlock.ProjectId);
            return View(projectBlock);
        }

        // GET: ProjectBlock/Edit/5
        public async Task<ActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ProjectBlock projectBlock = await db.ProjectBlocks.FindAsync(id);
            if (projectBlock == null)
            {
                return HttpNotFound();
            }
            ViewBag.ProjectId = new SelectList(db.Projects, "Id", "Name", projectBlock.ProjectId);
            return View(projectBlock);
        }

        // POST: ProjectBlock/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Edit([Bind(Include = "Id,ProjectId,BlockNo,DoorPlateBlockNo")] ProjectBlock projectBlock)
        {
            if (ModelState.IsValid)
            {
                db.Entry(projectBlock).State = EntityState.Modified;
                await db.SaveChangesAsync();
                return RedirectToAction("Index", new { id = projectBlock.ProjectId });
            }
            ViewBag.ProjectId = new SelectList(db.Projects, "Id", "Name", projectBlock.ProjectId);
            
            return View(projectBlock);
        }

        // GET: ProjectBlock/Delete/5
        public async Task<ActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ProjectBlock projectBlock = await db.ProjectBlocks.FindAsync(id);
            if (projectBlock == null)
            {
                return HttpNotFound();
            }
            return View(projectBlock);
        }

        // POST: ProjectBlock/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> DeleteConfirmed(int id)
        {
            ProjectBlock projectBlock = await db.ProjectBlocks.FindAsync(id);
            db.ProjectBlocks.Remove(projectBlock);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
